import requests
import re


def search(num):
    '''封装函数'''
    url = 'http://bj.58.com/chuzu/pn' + str(num) + '/'
    # 数据和头文件准备
    for i in range(1, 6):
        data = {'ClickID': i}

        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
        }
    try:
        # 发送请求，抓取信息
        res = requests.get(url, data=data, headers=headers)
        # 解析数据
        html = res.content.decode('utf-8')
        # 正则公示
        title = '''tongji_label.*\n.*\n.*>\n\s*(.*?)\s*</a>'''  # 标题
        img = '''lazy_src="//(.*?)"\n'''  # 图片地址
        room = '''<p class="room">(.*?)\s+(&nbsp;){4}(.*?)</p>'''  # 房间规格
        price = '''<b>(.*?)</b>(.*?)\s+'''  # 租金
        # 正则处理
        titlelist = re.findall(title, html)
        imglist = re.findall(img, html)
        roomlist = re.findall(room, html)
        pricelist = re.findall(price, html)
        # 输出显示
        for i in range(0, len(titlelist)):
            print(titlelist[i] + ' ' + roomlist[i][0] + ' ' +
                  roomlist[i][2] + ' ' + pricelist[i][0] + pricelist[i][1] + ' ' + '图片地址：' + imglist[i] + '\n')
    except Exception as e:
        if hasattr(e, 'code'):
            print('HTTPError')
            print(e.reason)
            print(e.code)
        elif hasattr(e, 'reason'):
            print(e.reason)
        else:
            print(e)


if __name__ == '__main__':
    i = 1
    search(i)
    print('当前第' + str(i) + '页')
    # 分页处理
    while True:
        num = input('是否继续查询下一页(y/n)？')
        if num == 'y':
            i = i + 1
            search(i)
            print('当前第' + str(i) +
                  '页---------------------------------------------------')
        if num == 'n':
            break
            print('退出')
